home *** CD-ROM | disk | FTP | other *** search
/ Nothing but Tetris / Nothing but Tetris.iso / amiga / yactris / src / makepieces.c < prev    next >
C/C++ Source or Header  |  1994-01-01  |  7KB  |  293 lines

  1. /*
  2.     YacTris v0.0
  3.     Copyright ⌐1993 Jonathan P. Springer
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 1, or (at your option)
  8.     any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.     For more details see the files README and COPYING, which should have
  20.     been included in this distribution.
  21.  
  22.     The author can be reached during the school year at these E-Mail addresses:
  23.  
  24.     springjp@screech.alfred.edu        (Internet)
  25.     springjp@ceramics.bitnet        (Bitnet)
  26.  
  27.     And can be reached by paper mail year-round at the following address:
  28.  
  29.     Jonathan Springer
  30.     360 W. Main St.
  31.     Dallastown, PA    17313-2014
  32.     USA
  33.  
  34. */
  35.  
  36.  
  37. /*
  38. **
  39. **  MakePieces.c
  40. **
  41. **  Program module containing the routines related to creating and freeing
  42. **  the images associated with each piece.
  43. **
  44. */
  45. #include <exec/types.h>
  46. #include <exec/memory.h>
  47. #include <dos/dos.h>
  48. #include <graphics/gfx.h>
  49. #include <graphics/gfxmacros.h>
  50. #include <graphics/gels.h>
  51. #include <graphics/rastport.h>
  52. #include <intuition/intuition.h>
  53. #include <math.h>
  54. #include <dbug.h>
  55. #include "yactris.h"
  56. #include <bruce.h>
  57.  
  58. #include <clib/exec_protos.h>
  59. #include <clib/graphics_protos.h>
  60. #include <clib/intuition_protos.h>
  61.  
  62. Prototype BOOL MakePieces(struct ScreenInfo *);
  63. Prototype BOOL GivePieceBitMap(struct PieceRot *, struct ScreenInfo *);
  64. Prototype void UnMakePieces(struct ScreenInfo *);
  65. Prototype void FreePieceBitMap(struct PieceRot *, struct ScreenInfo *);
  66. Prototype void DrawPiece(struct RastPort *, struct MakeStruct *, struct ScreenInfo *);
  67.  
  68. extern struct PieceRot Line[2];
  69. extern struct PieceRot Block;
  70. extern struct PieceRot Tee[4];
  71. extern struct PieceRot Zig[2];
  72. extern struct PieceRot Zag[2];
  73. extern struct PieceRot Ell[4];
  74. extern struct PieceRot Lee[4];
  75.  
  76. extern struct BitMap Sqbm;
  77. extern const struct MakeStruct Sq;
  78.  
  79. extern struct PieceRot *Pieces[];
  80.  
  81. extern struct Bob PBob;
  82. extern struct VSprite PVSprite;
  83.  
  84. extern struct Window *tWindow;
  85. extern struct Window *pWindow;
  86.  
  87. /*
  88. **
  89. **  MakePieces()
  90. **
  91. **  A brute force approach to creating bitmaps of the different pieces.
  92. **  I may go back and add some finesse to it later.
  93. **
  94. **  This routine will not notice if another bitmap already exists, meaning
  95. **  that you will lose bitmaps if you're not careful.
  96. */
  97. BOOL MakePieces(struct ScreenInfo *sInfo)
  98. {
  99.     struct RastPort rPort = {0};
  100.     struct TmpRas tRas = {0};
  101.     struct BitMap rBitMap;
  102.     UBYTE * tBuf = NULL;
  103.     int i, j;
  104.     UWORD areaBuffer[400] = {0};
  105.     struct AreaInfo aInfo;
  106.     const UWORD fill = 0xFFFF;
  107.     struct PieceRot *piece = NULL;
  108.     BOOL success = TRUE;
  109.  
  110.     DBUG_ENTER("MakePieces");
  111.  
  112.     /*    Initialize the raster port  */
  113.     InitRastPort(&rPort);
  114.  
  115.     /*    Init an AreaInfo for the Raster Port  */
  116.     InitArea(&aInfo, areaBuffer, 160);
  117.     rPort.AreaInfo = &aInfo;
  118.     rPort.DrawMode = JAM1;
  119.  
  120.     InitBitMap(&rBitMap,sInfo->planes,ceil(sInfo->xTimes/4)*16,sInfo->yTimes * 4);
  121.  
  122.     /*    Init a temporary Raster for the Raster Port  */
  123.     tBuf = AllocRaster(rBitMap.BytesPerRow * 8, rBitMap.Rows);
  124.     if (tBuf) {
  125.     InitTmpRas(&tRas, tBuf, rBitMap.BytesPerRow * rBitMap.Rows);
  126.     rPort.TmpRas = &tRas;
  127.     }
  128.  
  129.     /*    Finally, set the raster port's settings  */
  130.     SetAfPt(&rPort, &fill, 0);
  131.  
  132.     /*    And now, we can finally begin drawing  */
  133.     for (i=0; i<NUM_TYPES && success; i++) {
  134.     piece = Pieces[i];
  135.     do {
  136.         if ( GivePieceBitMap (piece, sInfo) ) {
  137.         rPort.BitMap = &piece->BitMap;
  138.         DrawPiece (&rPort, piece->ms, sInfo);
  139.         } else {
  140.         success = FALSE;
  141.         }
  142.  
  143.     } while (((piece=piece->next)!=Pieces[i]) && (success));
  144.     }
  145.  
  146.     /*    Let's draw the 1x1 square while we're at it.  */
  147.     InitBitMap(&Sqbm, sInfo->planes,
  148.         ceil(sInfo->xTimes/4)*16, sInfo->yTimes * 4);
  149.     for (i=0; i<sInfo->planes && success; i++)
  150.     if (!(Sqbm.Planes[i]=AllocRaster(Sqbm.BytesPerRow * 8, Sqbm.Rows)))
  151.         success = FALSE;
  152.     if (success) {
  153.     rPort.BitMap = &Sqbm;
  154.     DrawPiece (&rPort, &Sq, sInfo);
  155.     }
  156.  
  157.     /*    And clean-up, if we can remember what's dirty :)  */
  158.     if (tBuf) FreeRaster(tBuf, rBitMap.BytesPerRow * 8, rBitMap.Rows);
  159.  
  160.     DBUG_RETURN(success);
  161. }
  162.  
  163. /*
  164. **
  165. **  DrawPiece()
  166. **
  167. **  Given a raster port, an array of MakeStructs, and an array of pens, draws
  168. **  a playing piece.
  169. **
  170. */
  171. void DrawPiece(struct RastPort *rp, struct MakeStruct *MS, struct ScreenInfo *sInfo)
  172. {
  173.     struct MakeStruct *ms;
  174.  
  175.     DBUG_ENTER("DrawPiece");
  176.  
  177.     /*    Clear the raster  */
  178.     SetRast(rp, sInfo->pens[BACK]);
  179.  
  180.     for (ms=MS; ms->command != STOP; ms++) switch (ms->command) {
  181.  
  182.     case DRAW:
  183.         Draw(rp, ms->xMult*sInfo->xTimes+ms->xOffSet,
  184.              ms->yMult*sInfo->yTimes+ms->yOffSet);
  185.         break;
  186.  
  187.     case MOVE:
  188.         Move(rp, ms->xMult*sInfo->xTimes+ms->xOffSet,
  189.              ms->yMult*sInfo->yTimes+ms->yOffSet);
  190.         break;
  191.  
  192.     case FLOOD:
  193.         Flood(rp, 1, ms->xMult*sInfo->xTimes+ms->xOffSet,
  194.              ms->yMult*sInfo->yTimes+ms->yOffSet);
  195.         break;
  196.  
  197.     case PEN:
  198.         SetAPen(rp, sInfo->pens[ms->xMult]);
  199.         break;
  200.  
  201.     }
  202.     DBUG_VOID_RETURN;
  203. }
  204.  
  205. /*
  206. **
  207. **  GivePieceBitMap()
  208. **
  209. **  Allocate BitMap structure and space for a piece's bitmap and clear
  210. **  the bitmap.
  211. **
  212. */
  213. BOOL GivePieceBitMap(struct PieceRot *piece, struct ScreenInfo *sInfo)
  214. {
  215.     int i;
  216.     BOOL success = TRUE;
  217.  
  218.     DBUG_ENTER("GivePieceBitMap");
  219.  
  220.     DBUG_PRINT("GPBM",("xTimes: %2d,  yTimes: %2d,  planes: %2d", sInfo->xTimes,
  221.         sInfo->yTimes, sInfo->planes));
  222.  
  223.     InitBitMap(&piece->BitMap, sInfo->planes,
  224.         ceil(sInfo->xTimes/4)*16, sInfo->yTimes * 4);
  225.     for (i=0; i<sInfo->planes && success; i++) {
  226.     if (!(piece->BitMap.Planes[i]= AllocMem(
  227.         piece->BitMap.BytesPerRow*piece->BitMap.Rows, MEMF_CHIP|MEMF_CLEAR
  228.     ))) {
  229.         success = FALSE;
  230.     }
  231.     }
  232.  
  233.     DBUG_RETURN(success);
  234. }
  235.  
  236. /*
  237. **
  238. **  UnMakePieces()
  239. **
  240. **  Free the BitMaps for each of the Pieces
  241. **
  242. */
  243. void UnMakePieces(struct ScreenInfo *sInfo)
  244. {
  245.     int i;
  246.     struct PieceRot *piece;
  247.  
  248.     DBUG_ENTER("UnMakePieces");
  249.  
  250.     for (i=0; i<NUM_TYPES; i++) {
  251.     DBUG_PRINT("UMP",("Freeing piece %d", i));
  252.     piece = Pieces[i];
  253.     do {
  254.         FreePieceBitMap(piece, sInfo);
  255.         piece = piece->next;
  256.     } while (piece != Pieces[i]);
  257.     }
  258.  
  259.     for (i=0; i<sInfo->planes; i++) {
  260.     if (Sqbm.Planes[i])
  261.         FreeRaster(Sqbm.Planes[i], Sqbm.BytesPerRow*8, Sqbm.Rows);
  262.     Sqbm.Planes[i] = NULL;
  263.     }
  264.  
  265.  
  266.     DBUG_VOID_RETURN;
  267. }
  268.  
  269. /*
  270. **
  271. **  FreePieceBitMap()
  272. **
  273. **  Free all the memory allocated by GivePieceBitMap
  274. **
  275. */
  276. void FreePieceBitMap(struct PieceRot *piece, struct ScreenInfo *sInfo)
  277. {
  278.     int i;
  279.  
  280.     DBUG_ENTER("FreePieceBitMap");
  281.  
  282.     for (i=0; i<sInfo->planes; i++) {
  283.     if (piece->BitMap.Planes[i])
  284.         FreeMem(piece->BitMap.Planes[i],
  285.             piece->BitMap.BytesPerRow * piece->BitMap.Rows);
  286.     piece->BitMap.Planes[i] = NULL;
  287.     }
  288.  
  289.     DBUG_PRINT("FPBM",("BitMap.Planes NULLed"));
  290.  
  291.     DBUG_VOID_RETURN;
  292. }
  293.